* FILE LAST RUN ON 02/06/2024 USING STATA/SE 17.0

clear all
set maxvar 25000
set matsize 11000

cd ""
* Please change the directory according to your own set-up

/* TO RUN THIS FILE
1) Please download in a subfolder called CCES the CCES data for Stata from: https://cces.gov.harvard.edu/data
The following years need to be downloaded: 2012, 2014, 2018, 2020, and 2022
2) After unzipping each year, save in the CCES subfolder the dta files for each year under the name CCESyear where year is a two-digit number.
2/
*/

foreach year in 22 20 18 16 14 12  {
use .\CCES\CCES`year'.dta, clear

if `year' == 22 {
keep birthyr gender4 educ race inputstate employ marstat ownhome faminc CC`year'_4* inputstate
gen Male = 0 if gender4!=.
replace Male = 1 if gender4==1
}
if `year' == 20 | `year' == 18 | `year' == 16 {
keep  birthyr gender educ race inputstate employ marstat ownhome faminc CC`year'_4* inputstate
gen Male = 0 if gender!=.
replace Male = 1 if gender==1	
}
if `year'<16{
keep  birthyr gender educ race inputstate employ marstat ownhome faminc CC4* inputstate
gen Male = 0 if gender!=.
replace Male = 1 if gender==1	
	
}
gen White = 0 if race!=.
replace White =1 if race == 1
gen WhiteMale = 0 if race!=.
replace WhiteMale = 1 if White == 1 & Male == 1
label var WhiteMale "White Male"

gen Year = 2000 + `year'
gen Age = Year-birthyr
label var Age "Age"


if `year'>18 {
gen NoWhiteAdv = 1 if CC`year'_440a!=.
replace NoWhiteAdv = 0 if CC`year'_440a<4
* Dummy equals to one if somewhat or strongly disagree that whites have advantages
gen NoRacProb = 1 if CC`year'_440b!=.
replace NoRacProb = 0 if CC`year'_440b>2 & CC`year'_440b!=.
* Dummy equals to one if somewhat or strongly agree that racial problems are rare
gen RacialResent1 = 1  if CC`year'_441a!=.
replace RacialResent1 = 0 if CC`year'_441a>2 & CC`year'_441a!=.
* Dummy equals to one if somewhat or strongly agree that Blacks should overcome prejudice without special favour
gen RacialResent2 = 1 if CC`year'_441b!=.
replace RacialResent2 = 0 if CC`year'_441b<4
* Dummy equal to one if somewhat or strongly disagree that slavery and discrimination have
* created conditions that make it difficult for blacks to work their way out of the lower class.
}
if `year' == 18 {
gen NoWhiteAdv = 1 if CC`year'_422a!=.
replace NoWhiteAdv = 0 if CC`year'_422a<4
* Dummy equals to one if somewhat or strongly disagree that whites have advantages
gen NoRacProb = 1 if CC`year'_422b!=.
replace NoRacProb = 0 if CC`year'_422b>2 & CC`year'_422b!=.
* Dummy equals to one if somewhat or strongly agree that racial problems are rare
gen RacialResent1 = 1 if CC`year'_422e!=.
replace RacialResent1 = 0 if CC`year'_422e>2 & CC`year'_422e!=.
* Dummy equals to one if somewhat or strongly agree that Blacks should overcome prejudice without special favour
gen RacialResent2 = 1 if CC`year'_422f!=.
replace RacialResent2 = 0 if CC`year'_422f<4
* Dummy equal to one if somewhat or strongly disagree that slavery and discrimination have
* created conditions that make it difficult for blacks to work their way out of the lower class.
}
if `year' == 16 {
gen NoWhiteAdv = 1 if CC`year'_422d!=.
replace NoWhiteAdv = 0 if CC`year'_422d<4
* Dummy equals to one if somewhat or strongly disagree that whites have advantages
gen NoRacProb = 1 if CC`year'_422f!=.
replace NoRacProb = 0 if CC`year'_422f>2 & CC`year'_422f!=.
* Dummy equals to one if somewhat or strongly agree that racial problems are rare
}
if `year' < 16{
gen RacialResent1 = 1 if CC422a!=.
replace RacialResent1 = 0 if CC422a>2 & CC422a!=.
* Dummy equals to one if somewhat or strongly agree that Blacks should overcome prejudice without special favour
gen RacialResent2 = 1 if CC422b!=.
replace RacialResent2 = 0 if CC422b<4
* Dummy equal to one if somewhat or strongly disagree that slavery and discrimination have
* created conditions that make it difficult for blacks to work their way out of the lower class.
}



gen Own = 0 if ownhome !=.
replace Own = 1 if ownhome == 1
label var Own "Own house"

gen Married = 0 if marstat !=.
replace Married = 1 if marstat == 1 | marstat == 6
* Married or civil partnership
label var Married "Married"

gen Divorced = 0 if marstat !=.
replace Divorced = 1 if marstat == 2 | marstat == 3
* Divorced or separated
label var Divorced "Divorced"

forvalues j=1(1)9 {
		gen workstatus`j'=0 if employ!=.
		replace workstatus`j'=1 if employ == `j'
	}
	label var workstatus1 "Working full time"
	label var workstatus2 "Working part time"
	label var workstatus3 "Temporarily laid off"
	label var workstatus4 "Unemployed"
	label var workstatus5 "Retired"
	label var workstatus6 "Permanently disabled"
	label var workstatus7 "Homemaker"
	label var workstatus8 "Student"
	* Reference category is Other

forvalues j=1(1)16 {
	gen income`j' = 0 if faminc!=.
	replace income`j'=1 if faminc == `j'
	}
	gen income17 = 0 if faminc!=.
	replace income17 = 1 if faminc == 97
	* Reference category is prefer not to say
	* We exclude the small proportion who says 150,000 or more

	label var income1 "Income under USD10000 per year"
	forvalues j=2(1)8 {
		local bottom=10000*(`j'-1)
		local top =10000*(`j')-1
	label var income`j' "Income between USD`bottom' and USD`top' per year"
	}
	label var income9 "Income between USD80000 and USD99999 per year"
	label var income10 "Income between USD100000 and USD119999 per year"
	label var income11 "Income between USD120000 and USD149999 per year"
	label var income12 "Income between USD150000 and USD199999 per year"
	label var income13 "Income between USD200000 and USD249999 per year"
	label var income14 "Income between USD250000 and USD349999 per year"
	label var income15 "Income between USD350000 and USD499999 per year"
	label var income16 "Income over USD500000 per year"

gen HighSchool = 0 if educ!=.
replace HighSchool = 1 if educ == 2 | educ == 3
* High school or some college
label var HighSchool "High School diploma"
gen HigherEdu = 0  if educ!=.
replace HigherEdu = 1 if educ>3 & educ !=.
* Any university diploma incl. 2-y diploma
label var HigherEdu "University diploma"	
	


save .\CCES\CCES`year'_cleaned.dta, replace

}

use .\CCES\CCES22_cleaned.dta, clear
foreach year in 20 18 16 14 12  {
append using .\CCES\CCES`year'_cleaned.dta
}

keep NoWhiteAdv NoRacProb RacialResent1 RacialResent2 WhiteMale Male White HigherEdu HighSchool Own Married Divorced Age income1-income16 workstatus1-workstatus8 Year inputstate
order NoWhiteAdv NoRacProb RacialResent1 RacialResent2 WhiteMale Male White HigherEdu HighSchool Own Married Divorced Age income1-income16 workstatus1-workstatus8 Year inputstate

save .\CCES\CCES_final.dta, replace